package com.example.onlineeducation.ui.topic

import android.content.Context
import android.content.Intent
import android.view.View
import androidx.viewpager2.widget.ViewPager2
import com.example.onlineeducation.App
import com.example.onlineeducation.base.BaseActivity
import com.example.onlineeducation.databinding.StudyContentActivityBinding
import com.example.onlineeducation.utils.YcLoopCountDown
import com.example.onlineeducation.utils.YcTimeUtils
import com.example.onlineeducation.viewmodel.StudyContentVM
import com.google.android.material.tabs.TabLayoutMediator
import com.yc.jetpacklib.extension.ycIsNotEmpty
import com.yc.jetpacklib.recycleView.YcViewPagerAdapter

/**
 * SimpleDes:
 * Creator: Sindi
 * Date: 2022-04-06
 * UseDes:开始在线学习
 */
class StudyContentActivity :
    BaseActivity<StudyContentActivityBinding>(StudyContentActivityBinding::inflate) {
    var mChapterId: String? = null//当前章节id
    private val mViewModel: StudyContentVM by ycViewModels()
    private lateinit var mStudyMaterialFragment: StudyMaterialFragment

    companion object {
        private const val KEY_TOPIC_ID = "key_topic_id"
        private const val KEY_NEED_STUDY_TIME = "key_need_study_time"
        private const val KEY_STUDY_TIMES = "key_study_times"
        fun newInstance(context: Context, topicId: String, needStudyTime: Int, studyTimes: Int) {
            val intent = Intent(context, StudyContentActivity::class.java).apply {
                putExtra(KEY_TOPIC_ID, topicId)
                putExtra(KEY_NEED_STUDY_TIME, needStudyTime)
                putExtra(KEY_STUDY_TIMES, studyTimes)
            }
            context.startActivity(intent)
        }
    }

    private val mTimerLoop: YcLoopCountDown = YcLoopCountDown(this).apply {
        mPeriodTime = 1000L//刷新时间
        mPost.observe {
            mViewBinding.tvStudyTime.text = "学习时间:" + YcTimeUtils.intSecondToSecondStr(studyTimes)
            studyTimes += 1
        }
    }
    private val mUploadDataTimerLoop: YcLoopCountDown = YcLoopCountDown(this).apply {
        mPeriodTime = 60000L//刷新时间
        mPost.observe {
            if (mProId.ycIsNotEmpty()) {
                mViewModel.pushStudyLog(mProId!!, (studyTimes - currentStudyTimes).toString(), topicId)
                currentStudyTimes = studyTimes
            }
        }
    }
    private var studyTimes: Int = 0
    private var currentStudyTimes: Int = 0
    private var mProId: String? = null
    private lateinit var topicId: String

    fun switchStudyMaterialFragment() {
        mViewBinding.viewPager2.currentItem = 0
    }

    override fun StudyContentActivityBinding.initView() {
        ivActionbarLeft.setOnClickListener { finish() }
        topicId = intent.getStringExtra(KEY_TOPIC_ID)!!
        val needStudyTime = intent.getIntExtra(KEY_NEED_STUDY_TIME, 0)
        studyTimes = intent.getIntExtra(KEY_STUDY_TIMES, 0)
        currentStudyTimes = intent.getIntExtra(KEY_STUDY_TIMES, 0)
        tvStudyTime.text = "学习时间:" + YcTimeUtils.intSecondToSecondStr(studyTimes)
        tvReachTime.text = "达标时间:" + YcTimeUtils.intSecondToMinStr(needStudyTime)
        mStudyMaterialFragment = StudyMaterialFragment(topicId)
        val viewPagerAdapter = YcViewPagerAdapter(
            mutableListOf(
                mStudyMaterialFragment,
                StudyPractiseFragment(topicId)
            ), this@StudyContentActivity
        )

        viewPager2.adapter = viewPagerAdapter
        viewPager2.isUserInputEnabled = false
        TabLayoutMediator(tabLayout, viewPager2) { tab, position ->
            tab.text = mutableListOf("学习", "练习")[position]
        }.attach()

        viewPager2.registerOnPageChangeCallback(object : ViewPager2.OnPageChangeCallback() {
            override fun onPageSelected(position: Int) {
                if (position == 1) {
                    groupTime.visibility = View.GONE
                } else {
                    groupTime.visibility = View.VISIBLE
                }
            }
        })
        mViewModel.studyTimeLoop(true)
        mViewModel.mStudyTimeLoop.observe {
            if (it) {
                mTimerLoop.start()
                mUploadDataTimerLoop.start()
            }
        }
        App.mInstant.mViewModel.mProInfoEntity.observe {
            if (it != null && mProId != it.proId) {
                mProId = it.proId
            }
        }
    }

    override fun onDestroy() {
        App.mInstant.mPushLogViewModel.pushStudyLog(
            mProId!!,
            (studyTimes - currentStudyTimes).toString(),
            topicId
        )
        super.onDestroy()
    }

    override fun onBackPressed() {
        if (mStudyMaterialFragment.onBackPressed())
            return
        super.onBackPressed()
    }
}